package com.ihrm.system.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ihrm.common.utils.IdWorker;
import com.ihrm.common.utils.PermissionConstants;
import com.ihrm.domain.system.Permission;
import com.ihrm.domain.system.Role;
import com.ihrm.system.dao.PermissionDao;
import com.ihrm.system.dao.RoleDao;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 角色操作业务逻辑层
 */
@Service
public class RoleService {

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    /**
     * 分配权限
     */
    public void assignPerms(String roleId,List<String> permIds) {
        //1.获取分配的角色对象
        Role role = roleDao.selectById(roleId);
        //2.构造角色的权限集合
        Set<Permission> perms = new HashSet<>();
        for (String permId : permIds) {
            Permission permission = permissionDao.selectById(permId);
            //需要根据父id和类型查询API权限列表
            QueryWrapper<Permission> wrapper = new QueryWrapper<>();
            wrapper
                    .eq("type",PermissionConstants.PERMISSION_API)
                    .eq("permission_id",permission.getId());
            List<Permission> apiList = permissionDao.selectList(wrapper);
            //List<Permission> apiList = permissionDao.findByTypeAndPid(PermissionConstants.PERMISSION_API, permission.getId());
            perms.addAll(apiList);//自定赋予API权限
            perms.add(permission);//当前菜单或按钮的权限
        }
        System.out.println(perms.size());
        //3.设置角色和权限的关系
        role.setPermissions(perms);
        //4.更新角色
        roleDao.updateById(role);
    }

    /**
     * 添加角色
     */
    public void save(Role role) {
        //填充其他参数
        role.setId(idWorker.nextId() + "");
        roleDao.insert(role);
    }

    /**
     * 更新角色
     */
    public void update(Role role) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("id",role.getId());
        Role targer = roleDao.selectOne(wrapper);
        targer.setDescription(role.getDescription());
        targer.setName(role.getName());
        targer.setCompanyId(role.getCompanyId());
        roleDao.updateById(targer);
    }

    /**
     * 根据ID查询角色
     */
    public Role findById(String id) {
        return roleDao.selectById(id);
    }

    /**
     * 根据companyId 查询所有角色
     * @param companyId
     * @return
     */
    public List<Role> findAll(String companyId) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("company_Id",companyId);
        return roleDao.selectList(wrapper);
    }

    /**
     * 删除角色
     */
    public void delete(String id) {
        roleDao.deleteById(id);
    }


    /**
     * 分页查询角色
     * @param companyId
     * @param page
     * @param size
     * @return
     */
    public IPage<Role> findByPage(String companyId, int page, int size) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("company_Id",companyId);

        //SELECT COUNT(1) FROM pe_role WHERE companyId = ?

        Page<Role> page1 = new Page<>(page,size);
        IPage<Role> pageList = roleDao.selectPage(page1, wrapper);
        return pageList;
    }


}
